package com.gingko.duominuo.utils

import android.view.View
import android.widget.Checkable


/**
 * Describe：
 * @author bsx
 * CreateTime：2019/12/5
 */
// 扩展点击事件属性(重复点击时长)
var <T : View> T.lastClickTime: Long
    set(value) = setTag(1766613352, value)
    get() = getTag(1766613352) as? Long ?: 0

// 重复点击事件绑定
inline fun <T : View> T.singleClick(time: Long = 1000, crossinline block: (T) -> Unit) {
    setOnClickListener {
        val currentTimeMillis = System.currentTimeMillis()
        if (currentTimeMillis - this.lastClickTime > time || this is Checkable) {
            this.lastClickTime = currentTimeMillis
            block(this)
        }
    }
}

@Synchronized
fun <T : View> T.isFastClick(intervalTime: Long = 800): Boolean {
    val time = System.currentTimeMillis()
    if (time - this.lastClickTime < intervalTime) {
        this.lastClickTime = time
        return true
    }
    this.lastClickTime = time
    return false
}